import java.util.Arrays;

public class Test {

    /*
    * 复写零
    * */

    //先找到最后位置
    public static void main(String[] args) {
        int[] arr = {1,0,2,3,0,4,5,0};
        duplicateZeros(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void duplicateZeros(int[] arr){
        //先找到最后位置
        int cur = 0;
        int dest = -1;
        int n = arr.length;
        while(cur < n){
            if(arr[cur] == 0){
                dest += 2;
            } else{
                dest++;
            }
            if(dest >= n - 1){
                break;
            }
            cur++;
        }

        //处理边界情况
        if(dest == 0){
            arr[n - 1] = 0;
            dest-= 2;
            cur--;
        }

        //从后往前开始复写
        while(cur >= 0){
            if(arr[cur] != 0){
                arr[dest--] = arr[cur--];
            }else{
              arr[dest--] = 0;
              arr[dest--] = 0;
              cur--;
            }
        }
    }

}
